A heap is a binary tree in which every node is larger than the values associated with either child. A heap and a binary tree, for that matter, can be very efficiently stored in a vector, by placing the
children of node i in positions 2 * i + 1
and 2 * i + 2.
Using this
encoding, the largest value in the heap is
always located in the initial position, and can therefore be very efficiently retrieved.
In addition,
efficient logarithmic algorithms exist that permit a new element to be added
to a heap and the largest element removed from a heap. For these reasons, a
heap is a natural representation for the priority queue datatype.